package com.visizen.wisdomclass.server.client;

import com.visizen.wisdomclass.server.model.WcTopic;
import com.visizen.wisdomclass.server.model.WcTopicExample;

import java.util.List;

import com.visizen.wisdomclass.server.model.dto.TopicExt;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

public interface WcTopicMapper {
    /**
     * 获取热门话题的信息
     *
     * @param start
     * @param end
     * @return
     */
    @Select(" SELECT\n" +
            "  t.*\n" +
            " FROM wc_topic AS t\n" +
            " LIMIT #{start},#{end}")
    @Results({
            @Result(property = "topic_id", column = "topic_id"),
            @Result(property = "discussCount", column = "topic_id", one = @One(select = "selectDiscussCount")),
            @Result(property = "userCount", column = "topic_id", one = @One(select = "selectUserCount"))
    })
    List<TopicExt> topicList(@Param("start") int start,
                             @Param("end") int end);

    /**
     * 查询话题的讨论数
     *
     * @param topicId
     * @return
     */
    @Select("SELECT COUNT(*) FROM wc_discuss\n WHERE topic_id = #{topicId}")
    int selectDiscussCount(@Param("topicId") Long topicId);

    /**
     * 查询话题的参与人数
     *
     * @param topicId
     * @return
     */
    @Select(" SELECT\n" +
            "  count(*)\n" +
            " FROM (\n" +
            "  SELECT\n" +
            "    *\n" +
            "  FROM wc_discuss_log_see\n" +
            "  WHERE discuss_id IN (SELECT\n" +
            "                         discuss_id\n" +
            "                       FROM wc_discuss\n" +
            "                       WHERE topic_id = #{topicId})\n" +
            "  GROUP BY user_id) as a;")
    int selectUserCount(@Param("topicId") Long topicId);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_topic
     *
     * @mbggenerated
     */
    @SelectProvider(type = WcTopicSqlProvider.class, method = "countByExample")
    int countByExample(WcTopicExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_topic
     *
     * @mbggenerated
     */
    @DeleteProvider(type = WcTopicSqlProvider.class, method = "deleteByExample")
    int deleteByExample(WcTopicExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_topic
     *
     * @mbggenerated
     */
    @Delete({
            "delete from wc_topic",
            "where topic_id = #{topic_id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long topic_id);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_topic
     *
     * @mbggenerated
     */
    @Insert({
            "insert into wc_topic (topic_id, attach_id, ",
            "topic_name, topic_desc)",
            "values (#{topic_id,jdbcType=BIGINT}, #{attach_id,jdbcType=BIGINT}, ",
            "#{topic_name,jdbcType=VARCHAR}, #{topic_desc,jdbcType=LONGVARCHAR})"
    })
    int insert(WcTopic record);

    @Insert({
            "insert into wc_topic (topic_id, attach_id, ",
            "topic_name, topic_desc)",
            "values (#{topic_id,jdbcType=BIGINT}, #{attach_id,jdbcType=BIGINT}, ",
            "#{topic_name,jdbcType=VARCHAR}, #{topic_desc,jdbcType=LONGVARCHAR})"
    })
    @Options(useGeneratedKeys = true, keyProperty = "topic_id", keyColumn = "topic_id")
    int insertAndGenerateKeys(WcTopic record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_topic
     *
     * @mbggenerated
     */
    @InsertProvider(type = WcTopicSqlProvider.class, method = "insertSelective")
    int insertSelective(WcTopic record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_topic
     *
     * @mbggenerated
     */
    @SelectProvider(type = WcTopicSqlProvider.class, method = "selectByExampleWithBLOBs")
    @Results({
            @Result(column = "topic_id", property = "topic_id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "attach_id", property = "attach_id", jdbcType = JdbcType.BIGINT),
            @Result(column = "topic_name", property = "topic_name", jdbcType = JdbcType.VARCHAR),
            @Result(column = "topic_desc", property = "topic_desc", jdbcType = JdbcType.LONGVARCHAR)
    })
    List<WcTopic> selectByExampleWithBLOBs(WcTopicExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_topic
     *
     * @mbggenerated
     */
    @SelectProvider(type = WcTopicSqlProvider.class, method = "selectByExample")
    @Results({
            @Result(column = "topic_id", property = "topic_id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "attach_id", property = "attach_id", jdbcType = JdbcType.BIGINT),
            @Result(column = "topic_name", property = "topic_name", jdbcType = JdbcType.VARCHAR)
    })
    List<WcTopic> selectByExample(WcTopicExample example);

    @Select({
            "select",
            "topic_id, attach_id, topic_name, topic_desc",
            "from wc_topic",
            "where topic_id = #{topic_id,jdbcType=BIGINT}"
    })
    @Results({
            @Result(column = "topic_id", property = "topic_id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "attach_id", property = "attach_id", jdbcType = JdbcType.BIGINT),
            @Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
            @Result(column = "desc", property = "desc", jdbcType = JdbcType.LONGVARCHAR),
            @Result(property = "topic_id", column = "topic_id"),
            @Result(property = "discussCount", column = "topic_id", one = @One(select = "selectDiscussCount")),
            @Result(property = "userCount", column = "topic_id", one = @One(select = "selectUserCount"))
    })
    TopicExt selectExtByPrimaryKey(Long topic_id);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_topic
     *
     * @mbggenerated
     */
    @UpdateProvider(type = WcTopicSqlProvider.class, method = "updateByExampleSelective")
    int updateByExampleSelective(@Param("record") WcTopic record, @Param("example") WcTopicExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_topic
     *
     * @mbggenerated
     */
    @UpdateProvider(type = WcTopicSqlProvider.class, method = "updateByExampleWithBLOBs")
    int updateByExampleWithBLOBs(@Param("record") WcTopic record, @Param("example") WcTopicExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_topic
     *
     * @mbggenerated
     */
    @UpdateProvider(type = WcTopicSqlProvider.class, method = "updateByExample")
    int updateByExample(@Param("record") WcTopic record, @Param("example") WcTopicExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_topic
     *
     * @mbggenerated
     */
    @UpdateProvider(type = WcTopicSqlProvider.class, method = "updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(WcTopic record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_topic
     *
     * @mbggenerated
     */
    @Update({
            "update wc_topic",
            "set attach_id = #{attach_id,jdbcType=BIGINT},",
            "topic_name = #{topic_name,jdbcType=VARCHAR},",
            "topic_desc = #{topic_desc,jdbcType=LONGVARCHAR}",
            "where topic_id = #{topic_id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKeyWithBLOBs(WcTopic record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_topic
     *
     * @mbggenerated
     */
    @Update({
            "update wc_topic",
            "set attach_id = #{attach_id,jdbcType=BIGINT},",
            "topic_name = #{topic_name,jdbcType=VARCHAR}",
            "where topic_id = #{topic_id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(WcTopic record);
}